var DataProxy = require("data-proxy");
var Action = require("../apiaction");
var qs = require('querystring');
var day = 1000*60*60*24;
exports.loginRouter = function(router){


//

    //产品介绍
    router.get('/shapan/login/produce_intro',function(req,res){
        res.render ('shapan/login/produce_intro',{
            layout:'',
            active:'login',
            jscript:'shapan/login/produce_intro'
        });
    });
    //使用指南
    //router.get('/shapan/login/use_guide',function(req,res){
    //    res.render ('shapan/login/use_guide',{
    //        layout:'',
    //        active:'login',
    //        jscript:'shapan/login/use_guide'
    //    });
    //});
    //登入系统介绍
    router.get('/shapan/login/system_intro',function(req,res){
        res.render ('shapan/login/system_intro',{
            layout:'',
            active:'login',
            jscript:'shapan/login/system_intro'
        });
    });

    //登入
    router.get('/shapan/login/login',function(req,res){
        console.log('hygg')
        res.render ('shapan/login/login',{
            layout:'',
            active:'',
            jscript:'shapan/12login/login'
        });
    });

    //登录页面
    //router.get(['/','/shapan/login/login'], function(req, res) {
    //    res.render('login/login',{
    //        layout  : 'layout/layout-login',
    //        jscript : 'login/login'
    //    });
    //});

    router.get('/doLogin',function(req,res){
        var account = req.param("account");
        var pwd = req.param("pwd");
        //var remember = req.param("remember");
        var params= {account:account,pwd:pwd};
        var contents=qs.stringify(params);
        console.log(account,pwd);
        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/admin/adminLogin?"+contents,

                method:"GET",
                success:function(res,data){
                    var json = JSON.parse(data);
                    console.log("doLogin result:",json);
                    //console.log(req);
                    if(json.code == 0 ){
                        //send sessionid to client as cookie
                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send(data);
                    }
                }
            }
        }).handleRequest();
    });
//
//    router.get('/quite', function(req, res) {
//        console.log("退出成功");
//        res.clearCookie('user');
//        res.redirect('/');
//    });
//
//
//
//
//
//    router.get('/logout',function(req,res) {
//        var sessionid = req.cookies.sessionid;
//        new DataProxy({
//            req: req,
//            res: res,
//            reqType: "http",
//            reqOption: {
//                url: "/session/clear",
//                params: {sessionid: sessionid},
//                success: function (res, data) {
//                    res.clearCookie('sessionid', { path: '/' });
//                    res.clearCookie('userid', { path: '/' });
//                    res.clearCookie('username', { path: '/' });
//                    res.clearCookie('usermobile', { path: '/' });
//                    res.clearCookie('createdDate', { path: '/' });
//                    res.clearCookie('portrait', { path: '/' });
//                    res.redirect("/login");
//
//                }
//            }
//        }).handleRequest();
//    });
//
//
//    router.all('/doEditPwd', function(req, res) {
//        var password = req.param("password");
//        var oldpassword = req.param("oldpassword");
//        var sessionid = req.cookies.sessionid;
//
//        new DataProxy({
//            req:req,
//            res:res,
//            reqType:"http",
//            reqOption:{
//                url:"/login/updateUserPwd",
//                params:{oldPwd:oldpassword,newPwd:password,sessionid:sessionid},
//                success:function(res,d){
//                    res.status(200).send(d);
//                }
//            }
//        }).handleRequest();
//
//    });
//
}